package com.yr.ryjk.patient.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yr.ryjk.common.Constant;
import com.yr.ryjk.common.ErrorType;
import com.yr.ryjk.patient.dao.PatientMapper;
import com.yr.ryjk.patient.entity.Patient;
import com.yr.ryjk.patient.entity.PatientVo;
import com.yr.ryjk.redis.JedisUtil;
import com.yr.ryjk.user.dao.UserMapper;
import com.yr.ryjk.user.entity.User;
import com.yr.ryjk.util.IDGenerator;
import com.yr.ryjk.util.JSONUtil;

@Service
public class PatientService implements IPatientService{
	
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private PatientMapper patientMapper;
	
	@Override
	public Patient add(String token, String name, int gender,
			String birthday, String phone, int certificateType,
			String certificateNo, int isSelf) throws ParseException {
		String p=JedisUtil.getPhoneByToken(token);
		User user=userMapper.selectUserByPhone(p);	

		Patient patient=new Patient();
		patient.setId(IDGenerator.gen32UUID());
		patient.setUserId(user.getId());
		patient.setName(name);
		patient.setPhone(phone);
		patient.setGender(gender);
		patient.setCertificateNo(certificateNo);
		patient.setCertificateType(certificateType);
		patient.setIsSelf(isSelf);
		
		Date birDate = new SimpleDateFormat(Constant.DATE_FORMAT_YEAR).parse(birthday);			
		patient.setBirthday(birDate);
	
		patientMapper.insert(patient);
		return patientMapper.selectByPrimaryKey(patient.getId());
	}
	
	@Override
	public Map<String, Object> update(String token,String id, String name, int gender,
			String birthday, String phone, int certificateType,
			String certificateNo, int isSelf) throws ParseException {
		String p=JedisUtil.getPhoneByToken(token);
		User user=userMapper.selectUserByPhone(p);	
		List<Patient> list=patientMapper.selectPatientByUserId(user.getId());
		if(list.contains(id)){			
			Patient patient=patientMapper.selectByPrimaryKey(id);
			patient.setName(name);
			patient.setGender(gender);
			patient.setUserId(user.getId());
			patient.setCertificateNo(certificateNo);
			patient.setCertificateType(certificateType);
			patient.setPhone(phone);
			patient.setIsSelf(isSelf);
			
			Date birDate = new SimpleDateFormat(Constant.DATE_FORMAT_YEAR).parse(birthday);			
			patient.setBirthday(birDate);
			return JSONUtil.getJSON(ErrorType.SUCCESS);
		}else {
			return JSONUtil.getJSON(ErrorType.SERVER_ERROR);
		}	
	}
	
	@Override
	public void deleteById(String id) {
        patientMapper.deleteByPrimaryKey(id);		
	}
	
	@Override
	public Object getListAll(String token) {
		String p=JedisUtil.getPhoneByToken(token);
		if(p==null){
			return JSONUtil.getJSON(ErrorType.TOKEN_ERROR);
		}
		User user=userMapper.selectUserByPhone(p);
		if(user==null){
			return JSONUtil.getJSON(ErrorType.SERVER_ERROR);
		}
		List<PatientVo> list=patientMapper.selectByUserId(user.getId());
		return JSONUtil.getJSON(ErrorType.SUCCESS,list);
	}
}